//
//  BHApplyTeamUserViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/18.
//  队员列表

import UIKit

class BHApplyTeamUserViewController: BHBaseViewController {

    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .grouped)
        view.backgroundColor = .color_f8f8f8
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.showHeight)
        view.delegate = self
        view.dataSource = self
        return view
    }()
    
    var teamModel = BHUserGameApplyDetailModel.init()
    var applyId: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.bh_navigationBar.title_label.text = self.teamModel.teamName
//        self.bh_navigationBar.rightBtn.isHidden = false
//        self.bh_navigationBar.rightBtn.setTitle("退出战队", for: .normal)
//        self.bh_navigationBar.rightBtn.setTitleColor(.color_D43939, for: .normal)
//        self.bh_navigationBar.rightBtn.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
      
        self.regiseterCell()
        self.view.addSubview(self.tableView)
        
    }
    
//    override func rightBtnAction() {
//        BHCommonTool.showAlertWith(showVC: self, title: "提示", message: "退出战队=退出赛事，超过报名截止时间不允许队员主动退赛,是否确认退出当前战队？", okBtn: "确定", cancel: "再想想") { (confirm) in
//            if confirm == true {
//                self.teamUserQuiteGameService()
//            }
//        }
//    }
    

}
extension BHApplyTeamUserViewController {
    private func regiseterCell() {
        
        self.tableView.regiseterNib(nibName: "BHApplyTeamUserCell")
        self.tableView.regiseterNib(nibName: "BHApplyAdditionCell")

    }
}
extension BHApplyTeamUserViewController: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if self.teamModel.ifQuit == 1 {
            return 1
        }
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return self.teamModel.members.count
        }else {
            return 1
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 54
        }else {
            if self.teamModel.reminder == 0 {
                return 60
            }
            return 142
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 47
        }else {
            return CGFloat.leastNormalMagnitude
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.leastNormalMagnitude
        }else {
            return 55
        }
    }
    
}

extension BHApplyTeamUserViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            let back = UIView.init()
            back.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: 47)
            back.backgroundColor = .color_f8f8f8
            let view = BHApplyDetailSecView.loadFromXib()
            view.frame = CGRect(x: 0, y: 10, width: UIScreen.width, height: 37)
            view.sec_name_label.text = "组员"
            view.money_label.isHidden = true
            back.addSubview(view)
            return back
        }
        
        return nil
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section == 0 {
            return nil
        }
        let view = UIView.init()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: 55)
        view.backgroundColor = .color_f8f8f8
        let label = UILabel.init()
        label.frame = CGRect(x: 17, y: 20, width: UIScreen.width-34, height: 15)
        label.text = "退出战队=退出赛事，超过报名截止时间不允许队员主动退赛"
        label.font = .systemFont(ofSize: 12, weight: .medium)
        label.textColor = .color_999999
        view.addSubview(label)
        return view
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHApplyTeamUserCell", for: indexPath) as! BHApplyTeamUserCell
            let model = self.teamModel.members[indexPath.row]
            cell.name_label.text = model.name
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHApplyAdditionCell", for: indexPath) as! BHApplyAdditionCell
            if self.teamModel.reminder == 0 {
                cell.addition_btn.setTitle(" 已达到参赛报名人数要求，报名成功", for: .normal)
                cell.invite_btn.isHidden = true
            }else {
                cell.addition_btn.setTitle(" 还差\(self.teamModel.reminder)人即可报名成功", for: .normal)
                cell.invite_btn.isHidden = false
            }
            // 分享
            cell.invite_btn.addBtnAction(action: {[weak self] (_) in
                self?.share()
            }, for: .touchUpInside)
            return cell
        }
    }
}

extension BHApplyTeamUserViewController {
    func share() {
        BHShareTool.showShareMenuView { platform in
            BHShareTool.share_weblink(title: "赛事邀请", desText: "战队:"+self.teamModel.teamName+" 邀请您加入~", thumImage: "bh_app_share_icon", linkUrl: self.teamModel.shareUrl, platForm: platform)
        }
    }
}

// MARK: 队员退出比赛
extension BHApplyTeamUserViewController {
    func teamUserQuiteGameService()  {
        SVProgressHUD.show()
        let manager = BHHTTPClient.init()
        manager.get(url: String.me_myGame_quiteGame_url, paramters: ["id":self.applyId]) {[unowned self] (success, errorMsg, responseDic) in
            SVProgressHUD.dismiss()
            if success == true {
                BHToast.toast(text: "已退出赛事")
                DispatchQueue.main.asyncAfter(deadline: .now()+1.5) {
                    self.navigationController?.popToRootViewController(animated: true)
                }
               
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
    }
}
